
#Load packages
library(tidyverse)
library(haven)
library(Hmisc)


#LAPOP 18 Survey Data 
ecu.data <- as.data.frame(read_stata("Ecuador LAPOP AmericasBarometer 2016-17 V1.0_W.dta"))

#####Dependent Variables#### 

#Keep Dollar: 
#Do you think that the National Government should maintain dollarization or that should we have our own currency?
table(ecu.data$ecudolar, useNA = "ifany") #only 51 NAs 

#Coded:  0 = Have our own currency, 1 = Maintain dollarization 
ecu.data$keep.dollar <- as.numeric(ecu.data$ecudolar)
ecu.data$keep.dollar <- recode(ecu.data$keep.dollar, `2` = 0)

table(ecu.data$keep.dollar, ecu.data$ecudolar)

#Opinion Dollar: 
#In general terms, do you think that having the dollar as a currency has been something very good, good, bad or very bad for Ecuador?
table(ecu.data$ecudolar2, useNA = "ifany") #only 34 NAs  

#Coded: 0 = Very Bad, 1 = Bad, 2 = Good, 3 = Very good
ecu.data$opinion.dollar <-  4 - (as.numeric(ecu.data$ecudolar2))

#Make a factor version for visuals
ecu.data$opinion.dollar.fac <- recode_factor(ecu.data$opinion.dollar, 
                                      `0` = "Very Bad", `1` = "Bad", `2` = "Good", `3` = "Very Good" )


ecu.data$opinion.dollar.bi <- recode(ecu.data$opinion.dollar.fac, 
                                     "Very Bad" = 0, "Bad" = 0, "Good" = 1, "Very Good" = 1 )


#Variables about electronic money:

#Would you be willing to receive your salary or income in Electronic Money instead of dollars?
#0 = No,  1 = Yes
ecu.data$edollar <- recode(as.numeric(ecu.data$ecude3), `2` = 0)

#In general terms, do you think that having Electronic Money as a means of payment is something very good, good, bad or very bad for Ecuador?
ecu.data$edollar.good <- recode(as.numeric(ecu.data$ecude4), 
                                `1` = 1, `2` = 1, `3`= 0, `4` = 0)


######Independent Variables##### 

ecu.data$province <- as_factor(ecu.data$prov)


#Age in years 
ecu.data$age <- as.numeric(ecu.data$q2)

#Education in Years
ecu.data$edu <- as.numeric(ecu.data$ed)

#Age During Dollarization 
#Ecuador dollarizes in 2000
#survey fielded 2016 
ecu.data$age.2000 <- ecu.data$age - 16 

#Age During dollarization, factor variable
ecu.data$age.dollar <- cut(ecu.data$age.2000, 4, 
                           labels = c("0-20", "21-40", "41-60", "61-80"))

#18 or older during dollarization, dummy 
ecu.data$eighteen.in.2000 <- if_else(ecu.data$age.2000 > 17, 1, 0) 

#Receive remittances, 1 = yes, 0 = no
ecu.data$remittances <- as.numeric(ecu.data$q10a)
ecu.data$remittances <- recode(ecu.data$remittances, `2` = 0)


#Recieve Government welfare, 1 = yes, 0 = no
ecu.data$welfare <- as.numeric(ecu.data$wf1)
ecu.data$welfare <- recode(ecu.data$welfare, `2` = 0)

#Trust UN 
#3 = Very trustworthy, 0 = not trustworthy
ecu.data$trust.un <-  4 - as.numeric(ecu.data$mil10un)

#Trust US 
#3 = Very trustworthy, 0 = not trustworthy
ecu.data$trust.us <- 4 - as.numeric(ecu.data$mil10e)

#Pride in being ecuadorian
ecu.data$nat.pride <- as.numeric(ecu.data$b43)

#Ideology (L-R)
ecu.data$lr.ideology <- as.numeric(ecu.data$l1)

#Ideology factor
ecu.data$ideology.fac <- cut(ecu.data$lr.ideology, c(0,3, 7, 10), 
                             labels = c("Left", "Center", "Right"))

#Gender 
ecu.data$female <- recode(as.numeric(ecu.data$q1), `1` = 0, `2` = 1)

#Political Trust
ecu.data$trust.parties <- as.numeric(ecu.data$b21)
ecu.data$trust.leg <- as.numeric(ecu.data$b13)

#Respect institutions 
ecu.data$respect.instutions <- as.numeric(ecu.data$b2)


#Current occupation
#Combine "ocup1a" and "ocup4a"
ecu.data$ocup1a <- as.numeric(ecu.data$ocup1a)
ecu.data$ocup4a<- as.numeric(ecu.data$ocup4a)
ecu.data$occupation <- NA
ecu.data$occupation[ecu.data$ocup1a==1] <- "Gov. employee"
ecu.data$occupation[ecu.data$ocup1a==2] <- "Private employee"
ecu.data$occupation[ecu.data$ocup1a==3] <- "Business owner"
ecu.data$occupation[ecu.data$ocup1a==4] <- "Self-employed"
ecu.data$occupation[ecu.data$ocup1a==5] <- "Unpaid worker"
ecu.data$occupation[ecu.data$ocup4a==3] <- "Unemployed, looking for work"
ecu.data$occupation[ecu.data$ocup4a==7] <- "Unemployed, not looking for work"
ecu.data$occupation[ecu.data$ocup4a==4] <- "Student"
ecu.data$occupation[ecu.data$ocup4a==5] <- "Homemaker"
ecu.data$occupation[ecu.data$ocup4a==6] <- "Retired/Disabled"
ecu.data$occupation <- as.factor(ecu.data$occupation)

#Region 
ecu.data$region <- as_factor(ecu.data$estratopri)

#Urban Rural 
ecu.data$rural <- recode(as.numeric(ecu.data$ur), `1` = 0, `2` = 1)

#Own home 
ecu.data$own.home <- as.numeric(ecu.data$pr1)
ecu.data$own.home <- recode(ecu.data$own.home, `2` = 1, `1` = 0, `3` = 0, `4` = 0)


#Partisanship 
ecu.data$pid <- as.numeric(ecu.data$vb11)
ecu.data$pid.fac <- ifelse(ecu.data$pid == 913, "AP", 
                    ifelse(ecu.data$pid == 901, "CREO",
                    ifelse(ecu.data$pid == 903, "PSC", "Other")))

ecu.data$pid.fac[is.na(ecu.data$pid)] <- "None"
ecu.data$pid.fac <- factor(ecu.data$pid.fac, levels = c("None", "Other", "CREO", "PSC", "AP"))


table(ecu.data$pid.fac, ecu.data$pid, useNA = "ifany")

#Prefer state owned industry (ROS1)
ecu.data$pref.state.own  <- as.numeric(ecu.data$ros1)

#State should reduce inequality 
ecu.data$reduce.ineq <- as.numeric(ecu.data$ros4)

#Political Interest 
ecu.data$pol.interest <- 5 - as.numeric(ecu.data$pol1)

#Past vote choice 
ecu.data$recalled.vote <- as_factor(ecu.data$vb3n)

#Recalled Vote Slim
ecu.data <- ecu.data %>% 
  mutate(recalled.vote.slim = recode_factor(recalled.vote,
                                            "Not Applicable" = "Did not Vote/Null",
                                            "None (Blank ballot)" = "Did not Vote/Null",
                                            "None (Null ballot)" = "Did not Vote/Null",
                                            "Don't Know"  = NA_character_, 
                                            "No Response" = NA_character_,
                                            "Rafael Correa, Movimiento Alianza País – PAIS" = "Voted Correa",
                                            .default = "Voted Other Party"))
           
           
#Mothers Education
ecu.data$mother.edu <- as_factor(ecu.data$ed2)
ecu.data$mother.edu <- na_if(ecu.data$mother.edu, "Don't Know")

#News Consumption 
ecu.data$news_factor <- as_factor(ecu.data$gi0, ordered = F) 
ecu.data$news <- 5 - as.numeric(ecu.data$gi0)

ecu.data %>% 
  count(news, news_factor)

#Wealth Variable
assets <- c("r3", "r4", "r4a", "r5", "r6", "r7", "r8", "r12", "r14", "r15", "r18")
ecu.data <- drop_na(ecu.data, any_of(assets))

#Conduct PCA on assets 
wealth.pca <- ecu.data %>% 
  select(all_of(assets)) %>%
  princomp()

#Grab wealth as DF
wealth.pca <- data.frame(wealth.pca$scores)

#Rescale Wealth 
rescale01 <- function(x) {
  rng <- range(x, na.rm = TRUE)
  (x - rng[1]) / (rng[2] - rng[1])
}

wealth.pca$wealth <- rescale01(wealth.pca$Comp.1)
wealth.01 <- wealth.pca$wealth

#Bind wealth to the data
ecu.data <- cbind(ecu.data, wealth.01)

#Check face validity 
summary(lm(wealth.01 ~ edu + age, data = ecu.data))

#Wealth Quartiles 
ecu.data$wealth.qt <- cut2(ecu.data$wealth.01, g = 4)
ecu.data$wealth.qt <- factor(ecu.data$wealth.qt, labels =  c("1st", "2nd", "3rd", "4th"))


#Select all Variables
ecu.data <- ecu.data %>% 
  select(region, province, rural, keep.dollar, opinion.dollar, opinion.dollar.fac, news, news_factor,
         opinion.dollar.bi, edollar, edollar.good, age, edu,
         female, pid.fac, trust.leg, trust.parties, respect.instutions, 
         age.2000, age.dollar, eighteen.in.2000, occupation, own.home,
         remittances, welfare, nat.pride, lr.ideology, ideology.fac, pref.state.own,
         reduce.ineq, recalled.vote, recalled.vote.slim, mother.edu, pol.interest, wealth.01)


write_csv(ecu.data, "ecu_data_July22.csv")



